

/**
 * @author Johannes Blersch
 * 
 * @since 28.05.11
 * @version 10.05.11 - Johannes - Added the padding attribute for the FigurePanel constructor.
 * 
 * Generates the panel in which the user can select the figure, the name and the type of a player.
 */


package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import control.PlayerSettingsControl;


import model.Figure;


public class PlayerPanel extends JPanel{
	
	//variables
	private int displayedFigure;
	private ImageIcon[] images = new ImageIcon[6]; //Array with the figure images
		//Array with the figures
	private Figure[] figures = {new Figure("barrow", "Images/figures/barrow.png"), new Figure("cannon", "Images/figures/cannon.png"), new Figure("car", "Images/figures/car.png"), new Figure("dog", "Images/figures/dog.png"), new Figure("hat", "Images/figures/hat.png"), new Figure("iron", "Images/figures/iron.png")};
	
	
	//layout objects
	private FigurePanel[] figurePanels = new FigurePanel[6]; //Panels for displaying the figure images
	private JButton deletePlayerButton, nextFigureButton, lastFigureButton;
	private JPanel chooseFigurePanel;
	private JRadioButton menschRB, computerRB;
	private JTextField playerNameTextfield;
	
	////////////////////////////////////////////////////////////
	//// Constructor										////
	////////////////////////////////////////////////////////////
	
	PlayerPanel(){};
	
	/**
	 * Generates a player panel in which the user can type the settings for each player.
	 * 
	 * @param playerNumber The Number of the new Player
	 * @param thePlayerSettingsControl Reference to the PlayerSettings class for generating an association.
	 */
	PlayerPanel(int playerNumber, int imageNumber ,PlayerSettingsControl thePlayerSettingsControl){
		//Creation of the figure images must be done at first!
		createImages();
		
		displayedFigure = imageNumber;
		
		//Button images
		ImageIcon nextFigureImg = ImageCreator.createImageIcon("Images/Buttons/pfeil_rechts.png");
		ImageIcon lastFigureImg = ImageCreator.createImageIcon("Images/Buttons/pfeil_links.png");
		ImageIcon deletePlayerImg = ImageCreator.createImageIcon("Images/Buttons/SE_SL.png");
		
		//Buttons
		nextFigureButton = new JButton(nextFigureImg);
		nextFigureButton.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
		nextFigureButton.setContentAreaFilled(false);
		nextFigureButton.setActionCommand("nextFigurePlayer"+playerNumber);
		nextFigureButton.addActionListener(thePlayerSettingsControl);
		
		lastFigureButton = new JButton(lastFigureImg);
		lastFigureButton.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
		lastFigureButton.setContentAreaFilled(false);
		lastFigureButton.setActionCommand("lastFigurePlayer"+playerNumber);
		lastFigureButton.addActionListener(thePlayerSettingsControl);
		
		//Radio Buttons in a Button Group
		ButtonGroup radioGroup = new ButtonGroup();
		menschRB = new JRadioButton("Mensch");
		radioGroup.add(menschRB);
		computerRB = new JRadioButton("Computer");
		radioGroup.add(computerRB);
		
		menschRB.setSelected(true);
				
		this.setLayout(new GridLayout(4,1));
		
		//choosing the figure
		chooseFigurePanel = new JPanel(new BorderLayout());
		chooseFigurePanel.add(lastFigureButton, BorderLayout.WEST);
		chooseFigurePanel.add(figurePanels[imageNumber],BorderLayout.CENTER);
		chooseFigurePanel.add(nextFigureButton, BorderLayout.EAST);
		this.add(chooseFigurePanel);
		
		//player name input
		JPanel playerNamePanel = new JPanel(new GridLayout(2,1));
		playerNamePanel.add(new JLabel("Name Spieler "+playerNumber+":"));
		playerNameTextfield = new JTextField("Spieler "+playerNumber, 15); 
		playerNamePanel.add(playerNameTextfield);
		this.add(playerNamePanel);
		
		//player type choosing
		JPanel playerTypePanel = new JPanel(new GridLayout(2,1));
		playerTypePanel.setBorder(BorderFactory.createTitledBorder("Spielertyp"));
		playerTypePanel.add(menschRB);
		
		
		playerTypePanel.add(computerRB);
		this.add(playerTypePanel);		
		
		//delete player
		if (playerNumber > 2) {			
			deletePlayerButton = new JButton(deletePlayerImg);		
			deletePlayerButton.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
			deletePlayerButton.setContentAreaFilled(false);
			deletePlayerButton.setActionCommand("deletePlayer"+playerNumber);
			deletePlayerButton.addActionListener(thePlayerSettingsControl);
			this.add(deletePlayerButton);
		}
	}
	
	////////////////////////////////////////////////////////////
	//// Private-Methods									////
	////////////////////////////////////////////////////////////
	
	/**
	 * Creates the images of the figures for displaying in the gui.
	 */
	private void createImages() {		
		for (int i =0; i<6; i++)
		{		
			images[i] = ImageCreator.createImageIcon(figures[i].getImgPath());
			
			figurePanels[i] = new FigurePanel(images[i], 20); //10.05.11 - Johannes - Added 20
		}
	}	
	
	////////////////////////////////////////////////////////////
	//// Public-Methods										////
	////////////////////////////////////////////////////////////
	/**
	 * Displays the next figure image if the button next image became pressed.
	 */
	public void displayNewFigureImage(int nextFigure) {
		chooseFigurePanel.remove(figurePanels[displayedFigure]);
		displayedFigure = nextFigure;
		chooseFigurePanel.add(figurePanels[displayedFigure],BorderLayout.CENTER);
		chooseFigurePanel.validate();
		chooseFigurePanel.repaint();
	}
	
	////////////////////////////////////////////////////////////
	//// get-Methods										////
	////////////////////////////////////////////////////////////
	/**
	 * Returns the figure image of the player.
	 * @return Figure image.
	 */
	public ImageIcon getFigureImage() {
		return images[displayedFigure];
	}
	
	/**
	 * Returns the name of the player.
	 * @return Player name.
	 */
	public String getPlayerName() {
		return playerNameTextfield.getText();
	}
	
	/**
	 * Returns true if the player type is human and false if it is computer.
	 * @return Boolean value.
	 */
	public boolean isHuman() {
		if (true == menschRB.isSelected()) {
			return true;
		} else {
			return false;
		}
	}
	
	
	
	
}
